<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  table {
    border: 1px solid #eee;
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid #eee;
    padding: 10px;
  }
  th {
    background-color: #aaa;
  }
  .count {
    padding:0 5px;
  }
</style>
<body>
  <div>Header</div>
  <div id="app">app</div>
  <div>Footer</div>
  
  <script src="./format-util.js"></script>

  <!-- 1.引用react -->
  <script src="https://unpkg.com/react@16/umd/react.development.js"></script>
  <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
  <script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>

  <!-- 2.使用react -->
  <script type="text/babel">
    class App extends React.Component {
      constructor() {
        super()
        this.state = {
         books: [
             {
               id: 1,
               name: '《算法导论》',
               date: '2006-9',
               price: 85.00,
               count: 1
             },
             {
               id: 2,
               name: '《UNIX编程艺术》',
               date: '2006-2',
               price: 59.00,
               count: 1
             },
             {
               id: 3,
               name: '《编程珠玑》',
               date: '2008-10',
               price: 39.00,
               count: 1
             },
             {
               id: 4,
               name: '《代码大全》',
               date: '2006-3',
               price: 128.00,
               count: 1
             },
           ]
        }
      }
      render() {
        return (
          <div>
           <table>
               <thead>
                 <tr>
                   <th></th>
                   <th>书籍名称</th>
                   <th>出版日期</th>
                   <th>价格</th>
                   <th>购买数量</th>
                   <th>操作</th>
                 </tr>  
               </thead>
               <tbody>
                 {
                   this.state.books.map((item, index) => {
                     return (
                       <tr>
                         <td>{index + 1}</td>
                         <td>{item.name}</td>
                         <td>{item.date}</td>
                         <td>{formatPrice(item.price)}</td>
                         <td>
                           <button onClick={() => { this.decrement(index) }}>-</button>
                           <span className="count">{item.count}</span>  
                           <button onClick={() => { this.increment(index) }}>+</button>
                         </td>
                         <td><button onClick={() => this.remove(index)}>移除</button></td>
                       </tr> 
                     )
                   })
                 }
                  
               </tbody>
             </table>
           <h2>总价格:{this.total()}</h2>  
          </div>
        )
      }
      total() {
        // let total = 0
        // for(let item of this.state.books) {
        //  total += item.price * item.count
        // }
        // return formatPrice(total)
        const total = this.state.books.reduce((preValue, item) => {
          return preValue + item.count * item.price
        }, 0)
        return formatPrice(total)
      }
      increment(index) {
        // 拷贝数组
        const newBooks = [...this.state.books]
        newBooks[index].count += 1

        this.setState({
          books: newBooks
        })
      }
      decrement(index) {
        // 拷贝数组
        const newBooks = [...this.state.books]
        newBooks[index].count -= 1

        this.setState({
          books: newBooks
        })
      }
      remove(index) {
        this.setState({
          books: this.state.books.filter((item, indey) => index !== indey)
        })
        
      }
    }
   ReactDOM.render(<App/>, document.getElementById("app"))
  </script>
</body>
</html>
